minetest.register_node('ship:ladder', {
   description = 'Ladder',
   drawtype = 'mesh',
   mesh = 'ship_ladder.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   climbable = true,
   selection_box = {
      type = 'fixed',
         fixed = {
            {-.4, -.5, .3, .4, .5, .5},},
   },
   collision_box = {
      type = 'fixed',
         fixed = {
            {-.4, -.5, .3, .4, .5, .5},},
   },
   tiles = {'ship_rail_blank.png', 'ship_floor_blank.png'},
   groups = {breakable=1}
})

minetest.register_node('ship:door_H_1_0', {
   description = 'Closed Door',
   drawtype = 'mesh',
   mesh = 'ship_door_H_closed.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'ship_door_H_0.png'},
   groups = {breakable=1},
   selection_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, .5, 1.5, .1},},
   },
   collision_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, .5, 1.5, .1},},
   },
   after_place_node = function(pos)
      local above_pos = {x=pos.x, y=pos.y+1, z=pos.z}
      local above_name = minetest.get_node(above_pos).name
      if above_name == 'air' then
         minetest.set_node({x = pos.x, y = pos.y + 1, z = pos.z}, {name = 'doors:hidden'})
      end
   end,
   after_remove_node = function(pos)
      minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
   end,
   on_rightclick = function(pos, node)
      local fdir = node.param2
      local timer = minetest.get_node_timer(pos)
      timer:start(3)
      minetest.set_node(pos, {name = 'ship:door_H_1_1', param2=fdir})
      minetest.sound_play('ship_door_open', {pos = pos, max_hear_distance = 10, gain = 1})
      if fdir == 0 then --Search in the X axis
         local other_door_pos = {x=pos.x+1, y=pos.y, z=pos.z}
         local other_door = minetest.get_node(other_door_pos)
         if other_door.name == 'ship:door_H_1_0' then
            minetest.set_node(other_door_pos, {name = 'ship:door_H_1_1', param2=other_door.param2})
            local timer = minetest.get_node_timer(other_door_pos)
            timer:start(3)
         end
      elseif fdir == 1 then --Search in the Z axis
         local other_door_pos = {x=pos.x, y=pos.y, z=pos.z-1}
         local other_door = minetest.get_node(other_door_pos)
         if other_door.name == 'ship:door_H_1_0' then
            minetest.set_node(other_door_pos, {name = 'ship:door_H_1_1', param2=other_door.param2})
            local timer = minetest.get_node_timer(other_door_pos)
            timer:start(3)
         end
      elseif fdir == 2 then --Search in the X axis
         local other_door_pos = {x=pos.x-1, y=pos.y, z=pos.z}
         local other_door = minetest.get_node(other_door_pos)
         if other_door.name == 'ship:door_H_1_0' then
            minetest.set_node(other_door_pos, {name = 'ship:door_H_1_1', param2=other_door.param2})
            local timer = minetest.get_node_timer(other_door_pos)
            timer:start(3)
         end
      elseif fdir == 3 then --Search in the Z axis
         local other_door_pos = {x=pos.x, y=pos.y, z=pos.z+1}
         local other_door = minetest.get_node(other_door_pos)
         if other_door.name == 'ship:door_H_1_0' then
            minetest.set_node(other_door_pos, {name = 'ship:door_H_1_1', param2=other_door.param2})
            local timer = minetest.get_node_timer(other_door_pos)
            timer:start(3)
         end
      end
   end,
})

minetest.register_node('ship:door_H_1_1', {
   description = 'Open Door',
   drawtype = 'mesh',
   mesh = 'ship_door_H_open.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'ship_door_H_0.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'ship:door_H_1_0',
   selection_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, -.4, 1.5, .1},},
   },
   collision_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, -.4, 1.5, .1},},
   },
   after_place_node = function(pos)
      local above_pos = {x=pos.x, y=pos.y+1, z=pos.z}
      local above_name = minetest.get_node(above_pos).name
      if above_name == 'air' then
         minetest.set_node({x = pos.x, y = pos.y + 1, z = pos.z}, {name = 'doors:hidden'})
      end
   end,
   after_remove_node = function(pos)
      minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
   end,
   on_rightclick = function(pos, node)
      minetest.set_node(pos, {name = 'ship:door_H_1_0', param2=node.param2})
      minetest.sound_play('ship_door_close', {pos = pos, max_hear_distance = 10, gain = 1})
   end,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      minetest.set_node(pos, {name = 'ship:door_H_1_0', param2=node.param2})
      minetest.sound_play('ship_door_close', {pos = pos, max_hear_distance = 10, gain = .5})
   end
})

minetest.register_node('ship:door_V_1_0', { --Vertical opening, style 1, binary state 0 (closed)
   description = 'Closed Door',
   drawtype = 'mesh',
   mesh = 'ship_door_V_closed.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'ship_door_V_0.png'},
   groups = {breakable=1},
   selection_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, .5, 1.5, .1},},
   },
   collision_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, .5, 1.5, .1},},
   },
   after_place_node = function(pos)
      local above_pos = {x=pos.x, y=pos.y+1, z=pos.z}
      local above_name = minetest.get_node(above_pos).name
      if above_name == 'air' then
         minetest.set_node({x = pos.x, y = pos.y + 1, z = pos.z}, {name = 'doors:hidden'})
      end
   end,
   after_remove_node = function(pos)
      minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
   end,
   on_rightclick = function(pos, node)
      local fdir = node.param2
      local timer = minetest.get_node_timer(pos)
      timer:start(3)
      minetest.set_node(pos, {name = 'ship:door_V_1_1', param2=fdir})
      minetest.sound_play('ship_door_open', {pos = pos, max_hear_distance = 10, gain = 1})
      if fdir == 0 or fdir == 2 then --Search in the X axis
         local other_door_r_pos = {x=pos.x+1, y=pos.y, z=pos.z}
         local other_door_r = minetest.get_node(other_door_r_pos).name
         local other_door_l_pos = {x=pos.x-1, y=pos.y, z=pos.z}
         local other_door_l = minetest.get_node(other_door_l_pos).name
         while other_door_r == 'ship:door_V_1_0' do
            local timer = minetest.get_node_timer(other_door_r_pos)
            timer:start(3)
            minetest.set_node(other_door_r_pos, {name = 'ship:door_V_1_1', param2=fdir})
            other_door_r_pos.x = other_door_r_pos.x + 1
            other_door_r = minetest.get_node(other_door_r_pos).name
         end
         while other_door_l == 'ship:door_V_1_0' do
            local timer = minetest.get_node_timer(other_door_l_pos)
            timer:start(3)
            minetest.set_node(other_door_l_pos, {name = 'ship:door_V_1_1', param2=fdir})
            other_door_l_pos.x = other_door_l_pos.x - 1
            other_door_l = minetest.get_node(other_door_l_pos).name
         end
      elseif fdir == 1 or fdir == 3 then --Search in the Z axis
         local other_door_r_pos = {x=pos.x, y=pos.y, z=pos.z+1}
         local other_door_r = minetest.get_node(other_door_r_pos).name
         local other_door_l_pos = {x=pos.x, y=pos.y, z=pos.z-1}
         local other_door_l = minetest.get_node(other_door_l_pos).name
         while other_door_r == 'ship:door_V_1_0' do
            local timer = minetest.get_node_timer(other_door_r_pos)
            timer:start(3)
            minetest.set_node(other_door_r_pos, {name = 'ship:door_V_1_1', param2=fdir})
            other_door_r_pos.z = other_door_r_pos.z + 1
            other_door_r = minetest.get_node(other_door_r_pos).name
         end
         while other_door_l == 'ship:door_V_1_0' do
            local timer = minetest.get_node_timer(other_door_l_pos)
            timer:start(3)
            minetest.set_node(other_door_l_pos, {name = 'ship:door_V_1_1', param2=fdir})
            other_door_l_pos.z = other_door_l_pos.z - 1
            other_door_l = minetest.get_node(other_door_l_pos).name
         end
      end
   end,
})

minetest.register_node('ship:door_V_1_1', { --Vertical opening, style 1, binary state 1 (open)
   description = 'Open Door',
   drawtype = 'mesh',
   mesh = 'ship_door_V_open.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'ship_door_V_0.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'ship:door_H_1_0',
   selection_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, .5, -.4, .1},
            {-.5, 1.4, -.1, .5, 1.5, .1},},
   },
   collision_box = {
      type = 'fixed',
         fixed = {
            {-.5, -.5, -.1, .5, -.4, .1},
            {-.5, 1.4, -.1, .5, 1.5, .1},},
   },
   after_place_node = function(pos)
      local above_pos = {x=pos.x, y=pos.y+1, z=pos.z}
      local above_name = minetest.get_node(above_pos).name
      if above_name == 'air' then
         minetest.set_node({x = pos.x, y = pos.y + 1, z = pos.z}, {name = 'doors:hidden'})
      end
   end,
   after_remove_node = function(pos)
      minetest.remove_node({x = pos.x, y = pos.y + 1, z = pos.z})
   end,
   on_rightclick = function(pos, node)
      minetest.set_node(pos, {name = 'ship:door_V_1_0', param2=node.param2})
      minetest.sound_play('ship_door_close', {pos = pos, max_hear_distance = 10, gain = 1})
   end,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      minetest.set_node(pos, {name = 'ship:door_V_1_0', param2=node.param2})
      minetest.sound_play('ship_door_close', {pos = pos, max_hear_distance = 10, gain = .5})
   end
})

minetest.register_node('ship:door_3x3_open', {
   description = 'Open Door',
   drawtype = 'mesh',
   mesh = 'ship_door_3x3_open.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'ship_door_3x3_open.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'ship:door_3x3_closed',
   selection_box = {
      type = 'fixed',
         fixed = {
            {-2.5, -.5, -.1, .5, -.4, .1},
            {-2.5, 1, -.1, -2, 2.5, .1},
            {0, 1, -.1, .5, 2.5, .1},},
   },
   collision_box = {
      type = 'fixed',
         fixed = {
            {-2.5, -.5, -.1, .5, -.4, .1},
            {-2.5, 1, -.1, -2, 2.5, .1},
            {0, 1, -.1, .5, 2.5, .1},},
   },
   on_rightclick = function(pos, node)
      minetest.set_node(pos, {name = 'ship:door_3x3_closed', param2=node.param2})
      minetest.sound_play('ship_door_close', {pos = pos, max_hear_distance = 10, gain = 1})
   end,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      minetest.set_node(pos, {name = 'ship:door_3x3_closed', param2=node.param2})
      minetest.sound_play('ship_door_close', {pos = pos, max_hear_distance = 10, gain = .5})
   end
})

minetest.register_node('ship:door_3x3_closed', {
   description = 'Closed Door',
   drawtype = 'mesh',
   mesh = 'ship_door_3x3_closed.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   tiles = {'ship_door_3x3_closed.png'},
   groups = {breakable=1},
   selection_box = {
      type = 'fixed',
         fixed = {
            {-2.5, -.5, -.1, .5, 2.5, .1}},
   },
   collision_box = {
      type = 'fixed',
         fixed = {
            {-2.5, -.5, -.1, .5, 2.5, .1}},
   },
   on_rightclick = function(pos, node)
      local timer = minetest.get_node_timer(pos)
      timer:start(5)
      minetest.set_node(pos, {name = 'ship:door_3x3_open', param2=node.param2})
      minetest.sound_play('ship_door_open', {pos = pos, max_hear_distance = 10, gain = 1})
   end,
})

minetest.register_node('ship:energy_support', {
   description = 'Energy Support Beam',
   drawtype = 'mesh',
   mesh = 'ship_energy_support.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   light_source = 10,
   tiles = {'ship_energy_support.png'},
   use_texture_alpha = 'clip',
   groups = {breakable=1},
   selection_box = {
      type = 'fixed',
      fixed = {
         {-.35, -.5, -.35, .35, .4, .35},}
      },
   collision_box = {
      type = 'fixed',
      fixed = {
         {-.35, -.5, -.35, .35, .4, .35},}
      },
   after_place_node = function(pos, placer, itemstack)
      local node = minetest.get_node(pos)
      local above_pos = {x=pos.x, y=pos.y+1, z=pos.z}
      local above_name = minetest.get_node(above_pos).name
      local height = 0
      while above_name == 'air' and height < 20 do
         minetest.set_node(above_pos, {name = 'ship:energy_beam', param2 = node.param2})
         above_pos = {x=above_pos.x, y=above_pos.y+1, z=above_pos.z}
         above_name = minetest.get_node(above_pos).name
         height = height + 1
      end
      if above_name ~= 'air' then
         local pos = {x=above_pos.x, y=above_pos.y-1, z=above_pos.z}
         minetest.set_node(pos, {name = 'ship:energy_support_top', param2 = node.param2})
      end
   end,
   after_dig_node = function(pos, oldnode, oldmetadata, digger)
      local above_pos = {x=pos.x, y=pos.y+1, z=pos.z}
      local above_name = minetest.get_node(above_pos).name
      while above_name == 'ship:energy_beam' do
         minetest.remove_node(above_pos)
         above_pos = {x=above_pos.x, y=above_pos.y+1, z=above_pos.z}
         above_name = minetest.get_node(above_pos).name
      end
      if above_name == 'ship:energy_support_top' then
         minetest.remove_node(above_pos)
      end
   end,
})

local beam_select = {
   type = 'fixed',
      fixed = {
         {-.2, -.5, -.2, .2, .5, .2}}}

minetest.register_node('ship:energy_beam', {
   drawtype = 'mesh',
   mesh = 'ship_energy_beam.obj',
   paramtype = 'light',
   light_source = 10,
   tiles = {'ship_energy_beam.png'},
   use_texture_alpha = 'clip',
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = '',
   selection_box = beam_select,
   collision_box = beam_select,
})

minetest.register_node('ship:energy_support_top', {
   drawtype = 'mesh',
   mesh = 'ship_energy_support_top.obj',
   paramtype = 'light',
   paramtype2 = 'facedir',
   light_source = 10,
   tiles = {'ship_energy_support_top.png'},
   use_texture_alpha = 'clip',
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = '',
   selection_box = beam_select,
   collision_box = beam_select,
})
